package net.xo68.destiny.core.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * jwt令牌解析器
 * @author wuxie
 * @version 2020-11-6
 */
@Slf4j
public class JwtTokenParser {

    private final Claims claims;

    public JwtTokenParser(Claims claims) {
        this.claims = claims;
    }

    /**
     * 是否过期
     * @return
     */
    public boolean isExpiration() {
        try {
            return claims.getExpiration().before(new Date());
        } catch (ExpiredJwtException e) {
            return true;
        }
    }

    /**
     * 编号
     * @return
     */
    public String getId() {
        return claims.getId();
    }
    /**
     * 获得主题
     * @return
     */
    public String getSubject() {
        return claims.getSubject();
    }
    /**
     * token受众
     * @return
     */
    public String getAudience() {
        return claims.getAudience();
    }

    /**
     * 签发者
     * @return
     */
    public String getIssuer() {
        return claims.getIssuer();
    }

    /**
     * 签发时间
     * @return
     */
    public Date getIssuedAt() {
        return claims.getIssuedAt();
    }

    /**
     * 过期时间
     * @return
     */
    public Date getExpiration() {
        return claims.getExpiration();
    }

    public Object get(String claimName) {
        return claims.get(claimName);
    }

    public <T> T get(String claimName, Class<T> requiredType){
        return claims.get(claimName,requiredType);
    }
}
